package com.baijiaxing.weixin.family.common.http;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class HttpRequest {

	/**
	 * get方式请求返回状态码，如果成功 返回200
	 * 
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpRequestModel httpRequestGet(String url)
			throws ClientProtocolException, IOException {
		// 创建对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 获取get请求
		HttpGet httpGet = new HttpGet(url);
		// 进行get请求
		CloseableHttpResponse response = httpclient.execute(httpGet);
		// 基本的HTTP连接仍然通过响应对象举行
		// 获取状态
		int statusCode = response.getStatusLine().getStatusCode();
		// 类型
		HttpEntity entity = response.getEntity();
        System.out.println("entity========"+entity);
		HttpRequestModel httpRequestModel = new HttpRequestModel();
		httpRequestModel.setContent(EntityUtils.toString(entity, "UTF-8"));
		httpRequestModel.setContentCharSet(EntityUtils
				.getContentCharSet(entity));
		httpRequestModel.setContentMimeType(EntityUtils
				.getContentMimeType(entity));
		httpRequestModel.setStatusCode(statusCode);
		// 做点有用的响应体并且确保其完全消耗
		response.close();
		return httpRequestModel;
	}

	/**
	 * get方式请求返回状态码，如果成功 返回200
	 * 
	 * @param url
	 * @param parameters
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpEntity httpRequestGet2(String url)
			throws ClientProtocolException, IOException {
		// 创建对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 获取get请求
		HttpGet httpGet = new HttpGet(url);
		// 进行get请求
		CloseableHttpResponse response = httpclient.execute(httpGet);
		// 基本的HTTP连接仍然通过响应对象举行
		// 类型
		HttpEntity entity = response.getEntity();
		// 做点有用的响应体并且确保其完全消耗
//		response.close();
		return entity;
	}
	
	/**
	 * post方式请求返回状态码，如果成功 返回200
	 * 
	 * @param url
	 * @param parameters
	 * @return
	 * @throws IOException
	 */
	public static HttpRequestModel httpRequestPost(String url,
			List<NameValuePair> parameters) throws IOException {

		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.setEntity(new UrlEncodedFormEntity(parameters));
		CloseableHttpResponse response = httpclient.execute(httpPost);
		int statusCode = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		HttpRequestModel httpRequestModel = new HttpRequestModel();
		httpRequestModel.setContent(EntityUtils.toString(entity)); // 返回内容
		httpRequestModel.setContentCharSet(EntityUtils
				.getContentCharSet(entity)); // 内容字符集
		httpRequestModel.setContentMimeType(EntityUtils
				.getContentMimeType(entity)); // 内容类型
		httpRequestModel.setStatusCode(statusCode);
		// 做点有用的响应体并且确保其完全消耗
		EntityUtils.consume(entity);
		response.close();
		return httpRequestModel;
	}

	/**
	 * post方式请求返回状态码，如果成功 返回200
	 * 
	 * @param url
	 * @param parameters
	 * @return
	 * @throws IOException
	 */
	public static HttpRequestModel httpRequestPost(String url,
			List<NameValuePair> parameters, StringEntity stringEntity)
			throws IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader("User-Agent", "imgfornote"); // 是指浏览器,它的信息包括硬件平台、系统软件、应用软件和用户个人偏好。
		httpPost.setEntity(new UrlEncodedFormEntity(parameters));
		httpPost.setEntity(stringEntity);
		CloseableHttpResponse response = httpclient.execute(httpPost);
		int statusCode = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		HttpRequestModel httpRequestModel = new HttpRequestModel();
		httpRequestModel.setContent(EntityUtils.toString(entity)); // 返回内容
		httpRequestModel.setContentCharSet(EntityUtils
				.getContentCharSet(entity)); // 内容字符集
		httpRequestModel.setContentMimeType(EntityUtils
				.getContentMimeType(entity)); // 内容类型
		httpRequestModel.setStatusCode(statusCode);
		// 做点有用的响应体并且确保其完全消耗
		EntityUtils.consume(entity);
		response.close();
		return httpRequestModel;
	}

	/**
	 * 通过HttpPost SpringMvc上传
	 * 
	 * @param url
	 *            要上传的接口
	 * @param myfile
	 *            MultipartFile 流
	 * @return
	 * @throws IOException
	 */
	public static HttpRequestModel httpRequestPost(String url,
			MultipartFile myfile) throws IOException {
		CommonsMultipartFile cf = (CommonsMultipartFile) myfile;
		DiskFileItem fi = (DiskFileItem) cf.getFileItem();
		File file = fi.getStoreLocation();
		System.out.println("\t" + file.getName());
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		FileBody fileBody = new FileBody(file);
		MultipartEntity mulEntity = new MultipartEntity();
		mulEntity.addPart("buffer", fileBody);
		httpPost.setEntity(mulEntity);
		CloseableHttpResponse response = httpclient.execute(httpPost);
		int statusCode = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		HttpRequestModel httpRequestModel = new HttpRequestModel();
		httpRequestModel.setContent(EntityUtils.toString(entity)); // 返回内容
		httpRequestModel.setContentCharSet(EntityUtils
				.getContentCharSet(entity)); // 内容字符集
		httpRequestModel.setContentMimeType(EntityUtils
				.getContentMimeType(entity)); // 内容类型
		httpRequestModel.setStatusCode(statusCode);
		// 做点有用的响应体并且确保其完全消耗
		EntityUtils.consume(entity);
		response.close();
		return httpRequestModel;
	}

	/**
	 * 通过HttpPost SpringMvc上传
	 * 
	 * @param url
	 *            上传的Url接口
	 * @param tmpfile
	 *            零时文件目录
	 * @param myfile
	 *            MultipartFile流
	 * @param request
	 *            HttpServletRequest 流
	 * @return
	 * @throws IOException
	 */
	public static HttpRequestModel httpRequestPost(String url, String tmpfile,
			MultipartFile myfile, HttpServletRequest request)
			throws IOException {
		// 文件保存路径
		String filePath = request.getSession().getServletContext()
				.getRealPath("/")
				+ tmpfile + "/" + myfile.getOriginalFilename();
		// 转存文件
		myfile.transferTo(new File(filePath));
		File file = new File(filePath);
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		FileBody fileBody = new FileBody(file);
		MultipartEntity mulEntity = new MultipartEntity();
		mulEntity.addPart("buffer", fileBody);
		httpPost.setEntity(mulEntity);
		CloseableHttpResponse response = httpclient.execute(httpPost);
		int statusCode = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		HttpRequestModel httpRequestModel = new HttpRequestModel();
		httpRequestModel.setContent(EntityUtils.toString(entity)); // 返回内容
		httpRequestModel.setContentCharSet(EntityUtils.getContentCharSet(entity)); // 内容字符集
		httpRequestModel.setContentMimeType(EntityUtils.getContentMimeType(entity)); // 内容类型
		httpRequestModel.setStatusCode(statusCode);
		// 做点有用的响应体并且确保其完全消耗
		EntityUtils.consume(entity);
		response.close();
		return httpRequestModel;
	}

	/**
	 * 检查传
	 * 
	 * @param url 微信请求接口
	 * @param json json字符串
	 * @throws Exception
	 */
	public static HttpRequestModel httpPostWithJSON(String url, String json)
			throws Exception {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
		StringEntity se = new StringEntity(json);
		se.setContentEncoding("UTF-8");
		se.setContentType("application/json");
		httpPost.setEntity(se);
		CloseableHttpResponse response = httpclient.execute(httpPost);
		int statusCode = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		HttpRequestModel httpRequestModel = new HttpRequestModel();
		httpRequestModel.setContent(EntityUtils.toString(entity,"UTF-8")); // 返回内容
		httpRequestModel.setContentCharSet(EntityUtils.getContentCharSet(entity)); // 内容字符集
		httpRequestModel.setContentMimeType(EntityUtils.getContentMimeType(entity)); // 内容类型
		httpRequestModel.setStatusCode(statusCode);
		// 做点有用的响应体并且确保其完全消耗
		EntityUtils.consume(entity);
		response.close();
		return httpRequestModel;
	}
}
